package com.wangzhen.spring.component.scan;

import com.wangzhen.spring.component.scan.beans.UserController;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedType;
import java.util.Arrays;

@ComponentScan(basePackages = "com.wangzhen.spring.component.scan")
public class ComponentScanDemo {

    public static void main(String[] args) {

        Class<UserController> userControllerClass = UserController.class;
        AnnotatedType annotatedSuperclass = userControllerClass.getAnnotatedSuperclass();
        System.out.println(annotatedSuperclass);
        Arrays.stream(annotatedSuperclass.getAnnotations()).forEach(System.out::println);

        Component[] declaredAnnotationsByType = userControllerClass.getDeclaredAnnotationsByType(Component.class);
        Arrays.stream(declaredAnnotationsByType).forEach(System.err::println);
        System.out.println("****");
        Arrays.stream(userControllerClass.getAnnotatedInterfaces()).forEach(System.out::println);


        System.out.println("userControllerClass.getAnnotatedSuperclass():" + annotatedSuperclass);
        Annotation[] annotations =
                userControllerClass.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation.annotationType());
        }
        System.out.println("-----------");


        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        context.register(ComponentScanDemo.class);

        context.refresh();

        System.out.println(context.getBean(UserController.class));

        context.close();
    }
}
